#include "helpscene.h"

HelpScene::HelpScene(QWidget *parent)
    : GameScene(parent)
{
    background = new GameBackground(QPixmap(RESOURCE_DIR + "/Kit_HelpBackground.png"),mainScene);
    mainScene->setSceneRect(background->boundingRect());
    
    qreal scale = height()/mainScene->height();
    mainView->scale(scale,scale);
    mainView->centerOn(background);

    helpDisplay = new HelpDisplay(mainView,mainScene);

    returnButton = new HelpButton(QString("Return"),mainScene);
    returnButton->setFont(QFont("Arial Black",30,QFont::Black));
    QObject::connect(returnButton,SIGNAL(clicked(HelpButton*)),this,SLOT(buttonClicked(HelpButton*)));

    aboutButton = new HelpButton(QString("About"),mainScene);
    aboutButton->setPos(175,130);
    QObject::connect(aboutButton,SIGNAL(clicked(HelpButton*)),this,SLOT(buttonClicked(HelpButton*)));

    goalsButton = new HelpButton(QString("Game Goals"),mainScene);
    goalsButton->setPos(400,130);
    QObject::connect(goalsButton,SIGNAL(clicked(HelpButton*)),this,SLOT(buttonClicked(HelpButton*)));

    gameplayButton = new HelpButton(QString("Game Play"),mainScene);
    gameplayButton->setPos(745,130);
    QObject::connect(gameplayButton,SIGNAL(clicked(HelpButton*)),this,SLOT(buttonClicked(HelpButton*)));

    toolsButton = new HelpButton(QString("Tool Library"),mainScene);
    toolsButton->setPos(1045,130);
    QObject::connect(toolsButton,SIGNAL(clicked(HelpButton*)),this,SLOT(buttonClicked(HelpButton*)));

    QFile file(RESOURCE_DIR + "/about.html");
    QTextStream stream(&file);

    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;
    aboutString = QString(stream.readAll());

    file.close();
    file.setFileName(RESOURCE_DIR + "/gamegoals.html");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;
    goalsString = QString(stream.readAll());

    file.close();
    file.setFileName(RESOURCE_DIR + "/gameplay.html");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;
    gameplayString = QString(stream.readAll());

    file.close();
    file.setFileName(RESOURCE_DIR + "/toolslibrary.html");
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return;
    toolsString = QString(stream.readAll());

    aboutButton->setCurrentlyActive(true);
    activeButton = aboutButton;
    helpDisplay->setText(aboutString);

    setVisible(false);
}

void HelpScene::start()
{
    grabKeyboard();
    returnButton->setPos(mainView->mapToScene(width(),height()).x()-returnButton->boundingRect().width()-25,
                         mainView->mapToScene(width(),height()).y()-returnButton->boundingRect().height()-15);
    helpDisplay->setPos(mainView->mapToScene(0,0).x() + 60,190);
    helpDisplay->setRect(QRectF(0,0,helpDisplay->mapFromScene(mainView->mapToScene(width(),height())).x()-60,620));
    raise();
}

void HelpScene::setTab(Tabs tab)
{
    activeButton->setCurrentlyActive(false);

    if (tab == About)
    {
        activeButton = aboutButton;
        helpDisplay->setText(aboutString);
    }

    else if (tab == Goals)
    {
        activeButton = goalsButton;
        helpDisplay->setText(goalsString);
    }

    else if (tab == GamePlay)
    {
        activeButton = gameplayButton;
        helpDisplay->setText(gameplayString);
    }

    else if (tab == Tools)
    {
        activeButton = toolsButton;
        helpDisplay->setText(toolsString);
    }

    activeButton->setCurrentlyActive(true);
}

void HelpScene::buttonClicked(HelpButton *sender)
{
    if (sender == returnButton)
    {
        releaseKeyboard();
        endScene(GameScene::previousScene);
    }
    else
    {
        activeButton->setCurrentlyActive(false);
        sender->setCurrentlyActive(true);
        activeButton = sender;

        if (activeButton == aboutButton)
            helpDisplay->setText(aboutString);
        else if (activeButton == goalsButton)
            helpDisplay->setText(goalsString);
        else if (activeButton == gameplayButton)
            helpDisplay->setText(gameplayString);
        else if (activeButton == toolsButton)
            helpDisplay->setText(toolsString);
    }
}
